package com.yql.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.yql.entity.*;
import com.yql.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @GetMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @PostMapping("login")
    public String login(User user, HttpSession session){
        user = orderService.login(user);
        if (user!=null){
            session.setAttribute("uid",user.getUid());
            session.setAttribute("uname",user.getUname());
            session.setAttribute("user",user);
            return "index";
        }else{
            return "login";
        }
    }

    @GetMapping("index")
    public String index(){
        return "index";
    }

    @ResponseBody
    @PostMapping("getTree")
    public List<Menu> getTree(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Menu> list = orderService.getTree(user);
        return list;
    }

    @RequestMapping("list")
    public String list(OrderVO vo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "1") Integer pageSize,Model model){
        PageInfo<Order> info = orderService.listOrder(vo, pageNum, pageSize);
        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        return "list";
    }

    @RequestMapping("toAdd")
    public String toAdd(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        model.addAttribute("u",user);
        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        Long l = snowflake.nextId();
        model.addAttribute("l",l);
        return "add";
    }

    @ResponseBody
    @RequestMapping("/upFile")
    public String upFile(MultipartFile file) throws IOException {
        String path = "g:/pic/";
        if(null!=file && !file.isEmpty()){
            String originalFilename = file.getOriginalFilename();
            String s = UUID.randomUUID()  + file.getOriginalFilename().substring(originalFilename.lastIndexOf("."));
            System.out.println(s);
            File file1 = new File(path,s);
            file.transferTo(file1);
            return "/pic/"+s;
        }
        return null;
    }

    @RequestMapping("tj")
    public String tj(OrderVO vo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "1") Integer pageSize,Model model){
        PageInfo<Order> info = orderService.listOrder(vo, pageNum, pageSize);
        model.addAttribute("info",info);
        model.addAttribute("vo",vo);
        return "tj";
    }

    @PostMapping("add")
    public String add(Order order,HttpSession session){
        User user = (User) session.getAttribute("user");
        order.setUser(user);
        orderService.addOrder(order);
        return "redirect:fh";
    }

    @RequestMapping("fh")
    public String fh(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        model.addAttribute("u1",user);
        Snowflake snowflake = IdUtil.getSnowflake(31, 31);
        Long l1 = snowflake.nextId();
        model.addAttribute("l",l1);
        return "fh";
    }
}
